/*
 * @Author: HaiF
 * @Date: 2020-12-31 11:32:15
 * @LastEditTime: 2021-01-01 01:26:03
 * @LastEditors: your name
 * @Description:
 * @FilePath: \adminPC\src\store\modules\menus.js
 * @可以输入预定的版权声明、个性签名、空行等
 */

import { getListMenu, getChildren } from '@/api/menus'

const getDefaultState = () => {
  return {
    menus: [],
    menuBtns: [],
    currentId: ''
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_MENU: (state, menus) => {
    state.menus = menus
  },
  SET_BTN: (state, btns = []) => {
    state.menuBtns = btns
  },
  SET_CURRENTID: (state, menuId) => {
    state.currentId = menuId
  }
}

const actions = {
  getMenus({ commit }) {
    return new Promise((resolve, reject) => {
      getListMenu().then(res => {
        const { data, code } = res
        if (code === 200) {
          commit('SET_MENU', data)
          resolve(data)
        } else {
          reject([])
        }
      }).catch(() => {
        reject([])
      })
    })
  },

  getPageBtn({ dispatch, commit }, id) {
    getChildren({ parentId_eq: id }).then(res => {
      const { data, code } = res
      if (code === 200) {
        this.tableData = data
        commit('SET_BTN', data)
      }
    })
  },

  setMenuId({ commit }, menuId) {
    commit('SET_CURRENTID', menuId)
  }
}

export default {
  state,
  mutations,
  actions
}
